package com.ray.controller;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ray.bean.DatatablesViewPageBean;
import com.ray.bean.OrderProcedureTemplateBean;
import com.ray.bean.WorkshopProductionCycleBean;
import com.ray.exception.ServiceException;
import com.ray.service.BasicDataService;
import com.ray.service.OrderProcedureTemplateService;
import com.ray.util.ExcelExportUtil;
import com.ray.util.RequestUtil;
import com.ray.util.ResponseUtil;
import com.ray.util.ReturnStringUtil;


@Controller
@RequestMapping("/orderProcedureTemplate")
public class OrderProcedureTemplateController {
	@Autowired
	OrderProcedureTemplateService orderProcedureTemplateService;
	@Autowired
	BasicDataService basicDataService;
	
//	获取所有的车间和工序
	@RequestMapping("/getAllWorkShopAndProcedure")
	@ResponseBody
	public Map getAllWorkShopAndProcedure(HttpServletRequest request) throws ServiceException {
		Map map=RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(basicDataService.getAllWorkShopAndProcedure(map));
	}
	
//	添加模板
	@RequestMapping("/addTemplate")
	@ResponseBody
	public Map addTemplate(HttpServletRequest request) throws ServiceException {
		Map map=RequestUtil.getRequestAllParam(request);
		orderProcedureTemplateService.addTemplate(map);
		return ResponseUtil.ResponseResult(ReturnStringUtil.ADD_SUCCESS);
	}
	
//	添加用户工序
	@RequestMapping("/addUserProcedure")
	@ResponseBody
	public Map addUserProcedure(HttpServletRequest request) throws ServiceException {
		Map map=RequestUtil.getRequestAllParam(request);
		orderProcedureTemplateService.addUserProcedure(map);
		return ResponseUtil.ResponseResult(ReturnStringUtil.SET_SUCCESS);
	}
	
//	添加订单工序
	@RequestMapping("/addOrderProcedure")
	@ResponseBody
	public Map addOrderProcedure(HttpServletRequest request) throws ServiceException {
		Map map=RequestUtil.getRequestAllParam(request);
		orderProcedureTemplateService.addOrderProcedure(map);
		return ResponseUtil.ResponseResult(ReturnStringUtil.SET_SUCCESS);
	}
	
//	获取模板工序
	@RequestMapping("/getOrderProcedure")
	@ResponseBody
	public DatatablesViewPageBean getOrderProcedure(HttpServletRequest request) throws ServiceException {
		Map map=RequestUtil.getRequestAllParam(request);
		return orderProcedureTemplateService.getOrderProcedure(map);
	}
	
//	获取订单实际报工工序
	@RequestMapping("/getOrderRealProcedure")
	@ResponseBody
	public DatatablesViewPageBean getOrderRealProcedure(HttpServletRequest request) throws ServiceException {
		Map map=RequestUtil.getRequestAllParam(request);
		return orderProcedureTemplateService.getOrderRealProcedure(map);
	}
	
//	获取用户工序
	@RequestMapping("/getUserProcedure")
	@ResponseBody
	public DatatablesViewPageBean getUserProcedure(HttpServletRequest request) throws ServiceException {
		Map map=RequestUtil.getRequestAllParam(request);
		return orderProcedureTemplateService.getUserProcedure(map);
	}
	
//	删除模板
	@RequestMapping("/deleteTemplate")
	@ResponseBody
	public Map deleteTemplate(HttpServletRequest request) throws ServiceException {
		Map map=RequestUtil.getRequestAllParam(request);
		orderProcedureTemplateService.deleteTemplate(map);
		return ResponseUtil.ResponseResult(ReturnStringUtil.DELETE_SUCCESS);
	}
	
    //通过模板为订单设置工序
	@RequestMapping("/addOrderProcedureByTemp")
	@ResponseBody
	public Map addOrderProcedureByTemp(HttpServletRequest request) throws ServiceException {
		Map map=RequestUtil.getRequestAllParam(request);
		orderProcedureTemplateService.addOrderProcedureByTemp(map);
		return ResponseUtil.ResponseResult(ReturnStringUtil.SET_SUCCESS);
	}
	
   //获取所有模板
	@RequestMapping("/getAllTemp")
	@ResponseBody
	public Map getAllTemp(HttpServletRequest request) throws ServiceException {
		Map map=RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(orderProcedureTemplateService.getAllTemp(map));
	}
	
	
	//获取报工车间
	@RequestMapping("/getReportWorkShop")
	@ResponseBody
	public Map getReportWorkShop(HttpServletRequest request) throws ServiceException {
		Map map=RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(orderProcedureTemplateService.getReportWorkShop(map));
	}
	
	
	//获取报工车间
	@RequestMapping("/getInWorkShop")
	@ResponseBody
	public Map getInWorkShop(HttpServletRequest request) throws ServiceException {
		Map map=RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(orderProcedureTemplateService.getInWorkShop(map));
	}
	
	//获取报工工序
	@RequestMapping("/getReportProcedure")
	@ResponseBody
	public Map getReportProcedure(HttpServletRequest request) throws ServiceException {
		Map map=RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(orderProcedureTemplateService.getReportProcedure(map));
	}
	
	
	//移除模板工序
	@RequestMapping("/removeTemplateProcedure")
	@ResponseBody
	public Map removeTemplateProcedure(HttpServletRequest request) throws ServiceException {
		Map map=RequestUtil.getRequestAllParam(request);
		orderProcedureTemplateService.removeTemplateProcedure(map);
		return ResponseUtil.ResponseResult(ReturnStringUtil.REMOVE_SUCCESS);
	}
	
	//移除用户工序
	@RequestMapping("/removeUserProcedure")
	@ResponseBody
	public Map removeUserProcedure(HttpServletRequest request) throws ServiceException {
		Map map=RequestUtil.getRequestAllParam(request);
		orderProcedureTemplateService.removeUserProcedure(map);
		return ResponseUtil.ResponseResult(ReturnStringUtil.REMOVE_SUCCESS);
	}
	
	//移除模板名称
	@RequestMapping("/updateTempName")
	@ResponseBody
	public Map updateTempName(HttpServletRequest request) throws ServiceException {
		Map map=RequestUtil.getRequestAllParam(request);
		orderProcedureTemplateService.updateTempName(map);
		return ResponseUtil.ResponseResult(ReturnStringUtil.UPDATE_SUCCESS);
	}
	
	//获取单个订单工序
	@RequestMapping("/getOrderProcedureDetail")
	@ResponseBody
	public Map getOrderProcedureDetail(HttpServletRequest request) throws ServiceException {
		Map map=RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(orderProcedureTemplateService.getOrderHaveProcedure(map));
	}
	
	
	//获取单个订单工序
	@RequestMapping("/getNextProcedureName")
	@ResponseBody
	public Map getNextProcedureName(HttpServletRequest request) {
		Map map=RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(orderProcedureTemplateService.getNextProcedureName(map));
	}
	
	
	//移除模板工序
	@RequestMapping("/removeOrderProcedure")
	@ResponseBody
	public Map removeOrderProcedure(HttpServletRequest request) throws ServiceException {
		Map map=RequestUtil.getRequestAllParam(request);
		orderProcedureTemplateService.removeOrderProcedure(map);
		return ResponseUtil.ResponseResult(ReturnStringUtil.REMOVE_SUCCESS);
	}
	
	/**
	 * 设置订单生产顺序
	 * @param orderId(订单id)
	 * @param sort(顺序  订单查询页面为-1)
	 * orderProcedureTemplate
	 */
	@RequestMapping("/setOrderSort")
	@ResponseBody
	public Map setOrderSort(HttpServletRequest request) throws ServiceException {
		Map map=RequestUtil.getRequestAllParam(request);
		orderProcedureTemplateService.setOrderSort(map);
		return ResponseUtil.ResponseResult(ReturnStringUtil.SET_SUCCESS);
	}
	
	//保存打印详情
	@RequestMapping("/savePrintDetail")
	@ResponseBody
	public Map savePrintDetail(HttpServletRequest request) throws ServiceException {
		Map map=RequestUtil.getRequestAllParam(request);
		orderProcedureTemplateService.savePrintDetail(map);
		return ResponseUtil.ResponseResult(ReturnStringUtil.SAVE_SUCCESS);
	}
	
	
	//保存打印详情
	@RequestMapping("/getAleradyReportProcedure")
	@ResponseBody
	public Map getAleradyReportProcedure(HttpServletRequest request) throws ServiceException {
		Map map=RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(orderProcedureTemplateService.getAleradyReportProcedure(map));
	}
	
	
    //下载订单模板数据
	@RequestMapping(value="/downlodeOrderProcedure",method=RequestMethod.GET)
	@ResponseBody
	public void downlodeOrderProcedure(String orderId,HttpServletRequest request,HttpServletResponse response) throws ServiceException, CloneNotSupportedException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, IOException{
		Map map=RequestUtil.getRequestAllParam(request);
		String tempFolder=request.getServletContext().getRealPath("/")+"tempExcelFolder";
		File excelFile=orderProcedureTemplateService.createExcle(tempFolder,orderId,map);
		ResponseUtil.downloadfile(response, excelFile, "订单工序.xlsx", true);
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	//以下均为打印订单方法   本来不该写在这  有坑   为了赶时间没办法了 
	/**
	 * 获取订单主信息
	 * @param request
	 * @return
	 * @throws ServiceException
	 */
	@RequestMapping("/getOrderMain")
	@ResponseBody
	public Map getOrderMain(HttpServletRequest request,String orderId) throws ServiceException {
		Map map=RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(orderProcedureTemplateService.getOrderMain(map,orderId));
	}
	
	//获取订单物料详情
	@RequestMapping("/getMaterialDetail")
	@ResponseBody
	public Map getMaterialDetail(HttpServletRequest request,String orderId) throws ServiceException {
		Map map=RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(orderProcedureTemplateService.getMaterialDetail(map,orderId));
	}
	
	//获取订单实际报工工序
	@RequestMapping("/orderMagerProcedure")
	@ResponseBody
	public Map orderMagerProcedure(HttpServletRequest request,String orderId) throws ServiceException {
		Map map=RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(orderProcedureTemplateService.orderMagerProcedure(map,orderId));
	}
	
	//获取订单工序报工数据
	@RequestMapping("/getProcedureReport")
	@ResponseBody
	public Map getProcedureReport(HttpServletRequest request) throws ServiceException {
		Map map=RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(orderProcedureTemplateService.getProcedureReport(map));
	}
	
	//获取模板车间
	@RequestMapping("/getTempWorkShop")
	@ResponseBody
	public Map getTempWorkShop(HttpServletRequest request) throws ServiceException {
		Map map=RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(orderProcedureTemplateService.getTempWorkShop(map));
	}
	
	//将订单工序另存为模板
	@RequestMapping("/saveTemplateByOrderProcedure")
	@ResponseBody
	public Map saveTemplateByOrderProcedure(HttpServletRequest request) throws ServiceException {
		Map map=RequestUtil.getRequestAllParam(request);
		orderProcedureTemplateService.saveTemplateByOrderProcedure(map);
		return ResponseUtil.ResponseResult(ReturnStringUtil.SAVE_SUCCESS);
	}
		
	
	

}
